package com.example.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.entity.ExamClass;

@Mapper
public interface ExamClassMapper extends BaseMapper<ExamClass> {
    
    @Select("SELECT * FROM exam_classes WHERE exam_id = #{examId}")
    List<ExamClass> findByExamId(@Param("examId") Long examId);
    
    @Select("SELECT * FROM exam_classes WHERE class_id = #{classId}")
    List<ExamClass> findByClassId(@Param("classId") Long classId);
    
    @Select("SELECT * FROM exam_classes WHERE exam_id = #{examId} AND class_id = #{classId}")
    ExamClass findByExamIdAndClassId(@Param("examId") Long examId, @Param("classId") Long classId);
    
    @Delete("DELETE FROM exam_classes WHERE exam_id = #{examId} AND class_id = #{classId}")
    int deleteByExamIdAndClassId(@Param("examId") Long examId, @Param("classId") Long classId);
    
    @Delete("DELETE FROM exam_classes WHERE exam_id = #{examId}")
    int deleteByExamId(@Param("examId") Long examId);
    
    @Delete("DELETE FROM exam_classes WHERE class_id = #{classId}")
    int deleteByClassId(@Param("classId") Long classId);

    /**
     * 统计考试分配的学生总数
     */
    @Select("SELECT COUNT(DISTINCT u.id) FROM users u " +
            "INNER JOIN class_students cs ON u.id = cs.student_id " +
            "INNER JOIN exam_classes ec ON cs.class_id = ec.class_id " +
            "WHERE ec.exam_id = #{examId}")
    int countStudentsByExamId(@Param("examId") Long examId);
} 